'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async thumbscreate() {
    const { ctx } = this;
    const { uid, releaseid, tutorialid, fieldid, appointmentid } = ctx.request.body;
    if (!uid) {
      return ctx.body = { success: false, msg: '请传入uid' };
    }
    if (!(releaseid || tutorialid || fieldid || appointmentid)) {
      return ctx.body = { success: false, msg: '请传入点赞的作品id' };
    }
    const create = {};
    create.uid = uid;
    if (releaseid) {
      create.releaseid = releaseid;
    }
    if (tutorialid) {
      create.tutorialid = tutorialid;
    }
    if (fieldid) {
      create.fieldid = fieldid;
    }
    if (appointmentid) {
      create.appointmentid = appointmentid;
    }
    try {
      await ctx.model.Thumbs.create(
        create
      );
      ctx.body = { success: true, msg: '点赞成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '点赞失败' };
    }
  }

  async thumbsdelete() {
    const { ctx } = this;
    const { thumbsid, uid, releaseid, tutorialid, fieldid, appointmentid } = ctx.request.body;
    const where = {};
    if (!(thumbsid || uid)) {
      return ctx.body = { success: false, msg: '请传入必要查询字段' };
    }
    if (thumbsid) {
      where.thumbsid = thumbsid;
    }
    if (releaseid) {
      where.releaseid = releaseid;
    }
    if (tutorialid) {
      where.tutorialid = tutorialid;
    }
    if (fieldid) {
      where.fieldid = fieldid;
    }
    if (appointmentid) {
      where.appointmentid = appointmentid;
    }
    if (uid) {
      where.uid = uid;
    }
    try {
      await ctx.model.Thumbs.destroy({
        where,
      });
      ctx.body = { success: true, msg: '取消点赞' };
    } catch (e) {
      ctx.body = { success: false, msg: '取消点赞失败' };
    }
  }

  async thumbsfind() {
    const { ctx } = this;
    const { thumbsid, uid, releaseid, tutorialid, fieldid, appointmentid, page } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    const where = {};
    if (thumbsid) {
      where.thumbsid = thumbsid;
    }
    if (releaseid) {
      where.releaseid = releaseid;
    }
    if (tutorialid) {
      where.tutorialid = tutorialid;
    }
    if (fieldid) {
      where.fieldid = fieldid;
    }
    if (appointmentid) {
      where.appointmentid = appointmentid;
    }
    if (uid) {
      where.uid = uid;
    }
    try {
      const res = await ctx.model.Thumbs.findAndCountAll({
        where,
        limit,
        offset,
      });
      ctx.body = { success: true, data: res };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }

  async thumbslength() {
    const { ctx } = this;
    const { thumbsid, uid, releaseid, tutorialid, fieldid, appointmentid } = ctx.request.body;
    const where = {};
    if (thumbsid) {
      where.thumbsid = thumbsid;
    }
    if (releaseid) {
      where.releaseid = releaseid;
    }
    if (tutorialid) {
      where.tutorialid = tutorialid;
    }
    if (fieldid) {
      where.fieldid = fieldid;
    }
    if (appointmentid) {
      where.appointmentid = appointmentid;
    }
    if (uid) {
      where.uid = uid;
    }
    try {
      const res = await ctx.model.Thumbs.findAndCountAll({
        where,
      });
      ctx.body = { success: true, data: res.count };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }
}

module.exports = HomeController;
